package cz.najman.emdroid.ui;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.net.Uri;
import android.os.Bundle;
import android.preference.PreferenceManager;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;
import cz.najman.emdroid.R;
import cz.najman.emdroid.helper.UserHelper;
import cz.najman.emdroid.listener.OnDataUpdatedListener;
import cz.najman.emdroid.listener.ShowDialogOnDataUpdatedListener;
import cz.najman.emdroid.mappings.User;

/**
 * @author  Lukáš Najman
 */
public class LoginActivity extends Activity {

	private static final String TAG = "LoginActivity";
	
	private Button btnLogin;
	private EditText etEmail;

	private EditText etPassword;
	/**
	 */
	private EMDroidApplication mApplication;
	/**
	 */
	private UserHelper mUserHelper;
	private String mPassword;

	private String mEmail;
	/**
	 */
	private OnDataUpdatedListener mCustomListener;
	private SharedPreferences mPreferences;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login_credentials);
		mApplication = (EMDroidApplication) getApplication();		
		
		btnLogin = (Button) findViewById(R.id.btLogin);
		btnLogin.setOnClickListener(new LoginButtonOnClickListener());
		etEmail = (EditText) findViewById(R.id.etEmail);
		etPassword = (EditText) findViewById(R.id.etPassword);
		
		mUserHelper = mApplication.getUserHelper();
		mCustomListener = new LoginActivityListener(this);
		mPreferences = PreferenceManager.getDefaultSharedPreferences(this);
		
		etEmail.setText(mPreferences.getString("username", ""));
		etPassword.setText(mPreferences.getString("password", ""));	
		
	}
	
	@Override
	protected void onResume() {
		mUserHelper.registerListener(User.class.getCanonicalName(), mCustomListener);
		super.onResume();
	}
	
	@Override
	protected void onPause() {
		mUserHelper.unregisterListener(User.class.getCanonicalName());
		super.onPause();
	}
	
	
	class LoginButtonOnClickListener implements OnClickListener{

		@Override
		public void onClick(View v) {
			Log.d(TAG, "> LoginButtonOnClickListener > onClick");
			mEmail = etEmail.getText().toString();
			mPassword = etPassword.getText().toString();
			
			SharedPreferences.Editor editor = mPreferences.edit();
			editor.putString("username", mEmail);
			editor.putString("password", mPassword);
			editor.putBoolean("isUserLoged", false);
			editor.putInt("userId", -1);
			editor.commit();
			
			if((mEmail.length() != 0) && (mPassword.length() != 0)){
				mUserHelper.retrieveUserByEmail(mEmail);		
			}				
		}
		
	}
	
	public class LoginActivityListener extends ShowDialogOnDataUpdatedListener{

		public LoginActivityListener(Context context) {
			super(context);			
		}
		
		@Override
		protected void onFinished(Class objectId) {			
			Cursor c = getContentResolver().query(Uri.withAppendedPath(User.CONTENT_URI_USERS_BY_EMAIL, mEmail), null, null, null, null);
			
			c.moveToFirst();			
			
			String receivedPassword = c.getString(User.INDEX_PASSWORD);
			int userId = c.getInt(User.INDEX_ID);
			
			if(receivedPassword.equals(mPassword)){
				SharedPreferences.Editor editor = mPreferences.edit();
				editor.putBoolean("isUserLoged", true);
				editor.putInt("userId", userId);
				editor.commit();
				
				Intent intent = new Intent(LoginActivity.this, HomeActivity.class);
				startActivity(intent);
			} else {
				Toast.makeText(LoginActivity.this, "Bad credentials", Toast.LENGTH_SHORT).show();
			}
			
			super.onFinished(objectId);
		}
		
	}
	
}
